package com.arch.dev.client.view;

import java.util.List;
import java.util.Set;

import com.arch.dev.client.controller.BudgetController;
import com.arch.dev.client.view.ui.BudgetImageBundle;
import com.arch.dev.client.view.ui.ImageButton;
import com.arch.dev.client.view.ui.UsersTable;
import com.arch.dev.client.vo.BudgetVO;
import com.arch.dev.client.vo.UserVO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.MultiSelectionModel;

public class UsersView extends Composite{

	private static UsersViewUiBinder uiBinder = GWT
			.create(UsersViewUiBinder.class);

	interface UsersViewUiBinder extends UiBinder<Widget, UsersView> {
	}

	ImageButton deleteImageButton = null;
	
	@UiField
	UsersTable usersTable;

	public UsersView() {

		initWidget(uiBinder.createAndBindUi(this));

		BudgetImageBundle bundle = (BudgetImageBundle) GWT

				.create(BudgetImageBundle.class);
		
		ImageButton addImageButton  = new ImageButton(null, bundle.getAddIcon());
		
		final UserDialogView userDialogView = new UserDialogView(this);

		addImageButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				userDialogView.initFormFields();
				userDialogView.show();
			}
		});
		
		addImageButton.setTitle("Add Records");
		
		usersTable.addToolBarWidget(addImageButton);
		
		
		ImageButton editImageButton  = new ImageButton(null, bundle.getModifyIcon());
		
		final UserDialogView editUserDialogView = new UserDialogView(this);
		
		editImageButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				MultiSelectionModel<UserVO> multiSelectionModel = usersTable
						.getMultiSelectionModel();
				Set<UserVO> userVOs = multiSelectionModel.getSelectedSet();
				
				multiSelectionModel.clear();
				if(userVOs.size() == 1){
					editUserDialogView.populateFormFields(userVOs.iterator().next());
					editUserDialogView.show();
					//editBudgetDialogView.populateFields(budgetVOs.iterator().next());
				}else{
					Window.alert("Please select only one record");
				}
				

			}
		});
		
		editImageButton.setTitle("Edit Records");
		
		usersTable.addToolBarWidget(editImageButton);
		
		deleteImageButton  = new ImageButton(null, bundle.getDeleteIcon());

		deleteImageButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {

				MultiSelectionModel<UserVO> multiSelectionModel =  usersTable.getMultiSelectionModel();

				Set<UserVO> UserVOs = multiSelectionModel.getSelectedSet();			

			    BudgetController.getInstance().deleteUserValues(UsersView.this, UserVOs);
			    
			    multiSelectionModel.clear();

			}

		});

		deleteImageButton.setTitle("Delete Records");
		
		usersTable.addToolBarWidget(deleteImageButton);

		refresh();

	}
	
	public void refresh(){
		BudgetController.getInstance().getALLUsers(this);
	}
	
	public void populateTable(List<UserVO> UserVOs){
		usersTable.populateTable(UserVOs);
	}

}
